package com.example.bigevent.pojo;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class LoginUser implements UserDetails {

    private User user;
    private List<String> permissions;

    @Override
    @JsonIgnore  //redis为了安全考虑，默认不会把GrantedAuthority类型的数据序列化。
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority> authorityList = permissions.stream()
                .map(SimpleGrantedAuthority::new).collect(Collectors.toList());
        return authorityList;
    }

    @Override
    @JsonIgnore  //判断有没有属性 是根据有没有set或get方法来判断，如果不用这个注解，反序列化时会找不到password属性
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    @JsonIgnore
    public String getUsername() {
        return user.getUsername();
    }

    @Override
    @JsonIgnore
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    @JsonIgnore
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    @JsonIgnore
    public boolean isCredentialsNonExpired() {
        return true;
    }

    //用户是否可用
    @Override
    @JsonIgnore
    public boolean isEnabled() {
        return true;
    }
}
